package com.example.controlecarro;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.controlecarro.model.Carro;

public class MaincadastroActivity extends Activity {
	  private static final int INCLUIR = 0;
	    
	  
	  	Carro carro; 
	    EditText edtVeiculo;
	    EditText edtPlaca;
	    EditText edtAno;
	     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.maincadastro);
	         
	        try
	        {
	             
	         
	            final Bundle data = (Bundle) getIntent().getExtras();
	            int lint = data.getInt("tipo");
	            if (lint == INCLUIR)
	            {
	                
	                carro = new Carro(); 
	            }else{
	      
	                carro = (Carro)data.getSerializable("carro");
	            }
	             	    
	            edtVeiculo = (EditText)findViewById(R.id.edtVeiculo);
	            edtPlaca = (EditText)findViewById(R.id.edtPlaca);
	            edtAno = (EditText)findViewById(R.id.edtAno);
	     
	            edtVeiculo.setText(carro.getVeiculo());
	            edtPlaca.setText(carro.getPlaca());
	            edtAno.setText(carro.getAno());
	            
	        }catch (Exception e) {
	            trace("Erro : " + e.getMessage());
	        }             
	    }
	 
	    public void btnConfirmar_click(View view)
	    {
	        try
	        {
 
	            Intent data = new Intent();
	            carro.setVeiculo(edtVeiculo.getText().toString());
	            carro.setPlaca(edtPlaca.getText().toString());
	            carro.setAno(edtAno.getText().toString());
	            data.putExtra("carro", carro);
	            setResult(Activity.RESULT_OK, data);    
	            finish();
	        }catch (Exception e) {
	            trace("Erro : " + e.getMessage());
	        }             
	    }
	     
	    public void btnCancelar_click(View view)
	    {
	        try
	        {    
	            setResult(Activity.RESULT_CANCELED);
	            finish();
	        }catch (Exception e) {
	            trace("Erro : " + e.getMessage());
	        }             
	    }
	 
	    public void toast (String msg)
	    {
	        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
	    } 
	     
	    private void trace (String msg) 
	    {
	        toast (msg);
	    }    

}
